

# 本章是 数据容器

print("---------------列表的定义---------------")
# 字面量
["a", "b", "c"]
# 定义变量
var = ["a", "b", "c"]
# 定义空列表
var = []
var = list()
var = ["a", "b", 4, True, list()]
print(var)

print("---------列表的下标----------")
# 从后往前依次递减
name_list = ["tom", "jerry", "rose"]
print(name_list[-1])  # rose
print(name_list[-2])  # jerry
print(name_list[-3])  # tom
# 从前往后 0 依次递增
name_list=[[1,2,4],[4,5,6]]
print(name_list[0])  # [1, 2, 4]
print(name_list[1]) #[4, 5, 6]
print(name_list[0][0])
print(len(name_list))

print("------列表的查询功能--------")
my_list=["小明","小李","小红"]
index=my_list.index("小红")
print(index)


print("-----列表的修改方法------------")
my_list=["小明","小李","小红"]
my_list[0]="司军强"
print(my_list)



print("-------插入元素-------")
my_list=[1,2,3]
my_list.insert(1,"司军强")
print(my_list)

print("------追加元素----------")
my_list=[1,2,3]
my_list.append("兴达")
print(my_list)

print("-------追加元素-------")
my_list=[1,2,3]
my_list.extend(["a","b",True,False])
print(my_list)

print("-----删除元素----")
my_list=[1,2,3]
del my_list[0]
print(my_list)
my_list.pop(0)
print(my_list)

print("------------删除某个元素-----")
my_list=["a","b","c","b","d"]
my_list.remove("b")
print(my_list)

print("-------清空列表----")
my_list=["a","b","c","b","d"]
my_list.clear()
print(my_list)

print("---------统计某个元素在列表中的数量------")
my_list=["a","b","c","b","d","d","c"]
print(my_list.count("d"))

print("-----统计有多少个元素在列表中---------")
my_list=["a","b","c","b","d","d","c"]
print(len(my_list))


print("-------while循环------------")
my_list=["a","b","c","b","d","d","c"]
index=0
while index<len(my_list):
    # 这里是循环体对元素的处理
     num=my_list[index]
     print(num)
     index +=1



print("---for循环-----------")
my_list=["a","b","c","b","d","d","c"]
for num in my_list:
     print(num)


print("---------列表排序----------")
"""
扩展列表的sort方法
在学习了将函数作为参数传递后，我们可以学习列表的sort方法来对列表进行自定义排序
"""

# 准备列表
my_list = [["a", 33], ["b", 55], ["c", 11]]

# 排序，基于带名函数
# def choose_sort_key(element):
#     return element[1]
#
# my_list.sort(key=choose_sort_key, reverse=True)
# 排序，基于lambda匿名函数
my_list.sort(key=lambda element: element[1], reverse=True)

print(my_list)





print("--------元组---------------")
# 定义元组
my_tuple=("a","b",1,2,True,my_list)
print(my_tuple)

# 定义空元组
my_tuple=()
print(my_tuple)

my_tuple=tuple()
print(my_tuple)

# 定义1个元素的元组,后面一定要加 逗号 否则的话 不是元组
my_tuple=("hell",)
print(my_tuple)
print(type(my_tuple))




print("--------查询字符串--------")
my_str="what is happiness"
print(my_str.index("is")) # 5


print("------字符串的替换-------")
name="what is happiness"
new_name=name.replace("h","你好")
print(new_name)
print(name)

print("--------字符串分割------------")
name="小明, 小虎,小蓝"
name_list=name.split(",")
print(name_list)
print(type(name_list))

print("---------字符串去空--------")
name=" 给我讲个笑话吧 "
new_name=name.strip()
print(new_name)

print("-------字符串去掉指定的字符串------")
name="a给我讲个笑话吧         a"
print(name.strip("a"))


print("-----字符串统计出现的次数---------")
my_str="abdegdeddg"
print(my_str.count("d"))

len(my_str)


print("-------列表序列的切片------------")
#  定义一个序列
my_list=["a","b","c","d","e","f","j"]
# 开始给序列切片 从下标1开始，下标4结束(不含) 步长为1
new_my_list=my_list[1:4:2] # 从b开始取元素 每个间隔2个元素到d
print(new_my_list)
print(my_list.index("a"))

print("------元组列表的切片---------------")
# 定义元组序列
my_tuple=["a","b","c","d","e","f","j"]
# 开始切片 从头开始 到最后结束 步长为2
new_my_tuple=my_tuple[::2]
print(new_my_tuple)

print("-------负数-----------")
my_list = [1, 2, 3, 4, 5]
new_list = my_list[3:1:-1]	# 从下标3开始，到下标1（不含）结束，步长-1（倒序）
print(new_list)		# 结果：[4, 3]



print("--------set()集合的--------------")

my_set={"a","b","a","b","c"}
print(my_set)


print("-----set()集合添加元素-----------")
my_set={"hello","world"}
my_set.add("python")
print(my_set)

print("-----set()集合移除元素------")
my_set={"hello","world"}
my_set.remove("hello")
print(my_set)



print("------随机取出元素--------")
my_set={"hello","world","python"}
elemet=my_set.pop()
print(my_set)
print(elemet)

print("--------清空元素--------")
my_set={"hello","world","python"}
my_set.clear()
print(my_set)


print("------取出两个set集合的差集-------")
set_A={"a","b","c"}
set_b={"a","b","d"}
set_c=set_A.difference(set_b)
print(set_c) # c
print(set_A) # {'b', 'a', 'c'}
print(set_b) # {'b', 'd', 'a'}

print("------消除两个set集合差集--------")
set_a={"小明","小红","小兰"}
set_b={"小明","小红","小黑"}
set_a.difference_update(set_b)
print(set_a) # {'小兰'}
print(set_b) # {'小红', '小明', '小黑'}

print("-----两个集合的合并--------------")
set_a={"小明","小红","小兰"}
set_b={"小明","小红","小黑"}
set_c=set_a.union(set_b)
print(set_c) # {'小红', '小黑', '小明', '小兰'}
print(set_a) # {'小红', '小明', '小兰'}
print(set_b) # {'小黑', '小红', '小明'}


print("--------集合的长度------")
set_a={"小明","小红","小兰"}
print(len(set_a))

print('------集合的遍历-----------')
set_a={"小明","小红","小兰"}
for item in set_a:
    print(item)


print('-------------------------dic字典----------------------------')
#定义字典变量
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
# 定义空字典
my_dict={}
my_dict=dict()


print("-------------dict字典的取值-------------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
print(my_dict["熊大"]) # 外层取值
print(my_dict["熊大"]["英语"]) # 嵌套取值


print("-----------删除字典里面的元素----------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
my_dict.pop("age")
print(my_dict)

print("------清空字典--------------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
my_dict.clear()
print(my_dict)

print("---------获取全部的key------------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
keys=my_dict.keys()
print(keys)

print("----------遍历字典---------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
for key in my_dict.keys():
    print(f"key的名称:{key},value:{my_dict[key]}")

print("--------字典的长度-----------")
my_dict={"熊大":{"语文":77,"数学":88,"英语":55},"age":28,"set":True}
print(len(my_dict))

